﻿using System;
using System.Web;
using System.IO;
using System.Collections.Specialized;


using iSun.BLL.Base;
using iSun.Entity;

namespace iSun.Web.UI.Controls
{
    public class BaseHandler 
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        #endregion



        #region    自定义方法
        /// <summary>
        /// 实体类名[对应表名]
        /// </summary>
        public  virtual string EntityName
        {
            get
            {
                return GetStringFromQueryString("entity", "");
            }
        }

        public BaseBussnies _bll = null;
        public virtual void CreateBussniess(string entityName)
        {
            this._bll = BaseBussnies.CreateInstance(entityName);
        }


        /// <summary>
        /// 对应的编辑实体类
        /// </summary>
        public   virtual string EditEntity
        {
            get
            {
                string query = GetStringFromQueryString("editentity", "");
                if (query != "")
                {
                    return query;
                }
                return EntityName.Replace("V_", "").Replace("v_", "");
            }
        }




        public virtual string EntityId
        {
            get
            {
                return GetStringFromQueryString("entityid", "");
            }
        }

        public virtual HttpContext ZXContext
        {
            get
            {
                return System.Web.HttpContext.Current;
            }

        }

        /// <summary>
        /// 用于加载JS脚本使用
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public  virtual string LoadPageString(string filePath)
        {
            System.IO.StringWriter sw = new StringWriter();
            ZXContext.Server.Execute(filePath, sw);
            return sw.GetStringBuilder().ToString();
        }

        #region Account信息

        public virtual bool IsAuthenticated
        {
            get { return HttpContext.Current.User.Identity.IsAuthenticated; }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public virtual string UserName
        {
            get { return HttpContext.Current.User.Identity.Name; }
        }

        /// <summary>
        /// 用户ID[]
        /// </summary>
        public virtual Guid? UserId
        {
            get
            {
                if (User != null)
                {
                    return User.UserID;
                }
                else
                {
                    return null;
                }
            }
        }

        public virtual  Guid? FunctionID
        {
            get
            {
                return GetGuidFromQueryString("functionid");
            }
        }

        public virtual DatUser User
        {
            get
            {
                return System.Web.HttpContext.Current.Session["User"] as DatUser;
            }
        }

        /// <summary>
        /// 断定当前的功能权限
        /// </summary>
        /// <returns></returns>
        public virtual bool IsExistRight()
        {
            return false;
        }

        /// <summary>
        /// 创建ToolBarItems...
        /// </summary>
        public virtual void CreateToolBar()
        {
        }
        //在线试读功能 
        public virtual string GetDefCondition()
        {
            return "1=1";
        }
        #endregion

        #region URL Querystring 参数
        /// <summary>
        /// 获取参数集合
        /// </summary>
        /// <returns></returns>
        public virtual  NameValueCollection GetQueryString()
        {
            return new NameValueCollection(HttpContext.Current.Request.QueryString);
        }

        /// <summary>
        /// Allows access to QueryString values
        /// </summary>
        private  NameValueCollection _queryString;
        public virtual NameValueCollection QueryString
        {
            get
            {
                return GetQueryString();
            }
        }


        // *********************************************************************
        //  获取Guid参数
        /// <summary>
        /// Retrieves a value from the query string and returns it as an int.
        /// </summary>
        // ***********************************************************************/
        public virtual Guid GetGuidFromQueryString(string key)
        {
            Guid returnValue = Guid.Empty;
            string queryStringValue;

            // Attempt to get the value from the query string
            //
            queryStringValue = QueryString[key];

            // If we didn't find anything, just return
            //
            if (queryStringValue == null)
                return returnValue;

            // Found a value, attempt to conver to integer
            //
            try
            {

                // Special case if we find a # in the value
                //
                if (queryStringValue.IndexOf("#") > 0)
                    queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

                returnValue = new Guid(queryStringValue);
            }
            catch { }

            return returnValue;

        }

        // *********************************************************************
        //  获取整型参数
        //
        /// <summary>
        /// Retrieves a value from the query string and returns it as an int.
        /// </summary>
        // ***********************************************************************/
        public virtual int GetIntFromQueryString(string key, int defaultValue)
        {
            string queryStringValue;


            // Attempt to get the value from the query string
            //
            queryStringValue = this.QueryString[key];

            // If we didn't find anything, just return
            //
            if (queryStringValue == null)
                return defaultValue;

            // Found a value, attempt to conver to integer
            //
            try
            {

                // Special case if we find a # in the value
                //
                if (queryStringValue.IndexOf("#") > 0)
                    queryStringValue = queryStringValue.Substring(0, queryStringValue.IndexOf("#"));

                defaultValue = Convert.ToInt32(queryStringValue);
            }
            catch { }

            return defaultValue;

        }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public virtual string GetStringFromQueryString(string key, string defaultValue)
        {
            string queryStringValue;


            // Attempt to get the value from the query string
            queryStringValue = this.QueryString[key];

            // If we didn't find anything, just return
            //
            if (queryStringValue == null)
                return defaultValue;

            return queryStringValue;

        }

        public virtual void WriteExpLog()
        {
            // Exception x = Server.GetLastError().GetBaseException();
            //LogBusiness buss = new LogBusiness();
            //LogBusiness.WriteExceptionLog(x, this.User);
        }
        #endregion

        #endregion
    }
}
